package org.ielts.action;

import static com.yifeng.struts2.ResultParameters.*;
import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ActionUtils.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.ielts.service.ConsultantService;
import org.ielts.service.CourseService;
import org.ielts.service.FinancService;
import org.ielts.service.SignCourseLogService;
import org.ielts.service.SignCourseService;
import org.ielts.service.StudentService;
import org.ielts.service.security.UserService;
import org.ielts.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.yifeng.ProfileContext;
import com.yifeng.modules.annotations.Operation;
import com.yifeng.profile.Profile;
import com.yifeng.utils.JsonResultBuilder;
import com.yifeng.utils.PageUtils;
import com.yifeng.web.Pagers;
import com.yifeng.web.Paginations;

public class FinanceAction extends BaseAction {

	@Autowired
	StudentService studentService;
	@Autowired
	SignCourseService signCourseService;

	@Autowired
	UserService userService;

	@Autowired
	SignCourseLogService signCourseLogService;

	private String name;
	private Boolean checked;
	private String[] constansId;
	private int check=0;
	
	@Operation(result = FILL)
	public String listSignCourseLog() {

		Pageable pageable = getPageable();
		
		
		put("constants", consultantService.listConsultant());

		Page<SignCourseLog> signCourseLogs = signCourseLogService.search(name, checked, constansId,pageable);
		
		put("signCourseLogs", signCourseLogs);
		put(PAGINATIONS, new Paginations(signCourseLogs));

		return FILL;
	}

	@Operation(result = FILL, operationText = "客户注册课程")
	public String preSignUp() {
		Iterable<Student> students = null;

		String stuId = form.readString("stuId");
		if (StringUtils.isNotBlank(stuId)) {
			stuId = stuId.trim();
			students = studentService.findByStuId(stuId);
		}

		String name = form.readString("name");
		if (StringUtils.isNotBlank(name)) {
			students = studentService.findByName(name);
		}

		put("students", students);

		return FILL;
	}

	@Operation(result = FILL, operationText = "客户注册课程")
	public String signUp() {
		String id = form.readString("id");

		Student student = studentService.findOne(id);
		put("student", student);

		Iterable<Course> courses = courseService.findAll();
		put("courses", courses);

		Iterable<User> consultants = consultantService.listConsultant();
		put("consultants", consultants);

		put("begin", DateUtil.getAsString(DateUtil.now(), "yyyy-MM-dd"));
		put("end", DateUtil.getAsString(DateUtil.nextYear(), "yyyy-MM-dd"));

		return FILL;
	}

	@Operation(result = "JSON")
	public String dosignUp() {
		// 学员编号
		String id = form.readString("id");

		String stuId = form.readString("stuId");

		if (StringUtils.isNotBlank(stuId)) {
			studentService.changeStuId(id, stuId);
		}

		String courseId = form.readString("courseId");
		String contractId = form.readString("contractId");
		String cardId = form.readString("cardId");
		String title = form.readString("title");
		String lessonCard = form.readString("lessonCard");
		int times = valueReader.name("times").defaultValue(0).intValue();

		String consultantId = form.readString("consultantId");

		Date beginTime = valueReader.name("beginTime").dateValue("yyyy-MM-dd");
		Date endTime = valueReader.name("endTime").dateValue("yyyy-MM-dd");

		String book = form.readString("book");
		String note = form.readString("note");

		// Date signTime = valueReader.name("signTime").dateValue("yyyy-MM-dd");

		SignCourseLog clog = new SignCourseLog();
		clog.setEndTime(endTime);
		clog.setBeginTime(beginTime);
		clog.setTimes(times);
		clog.setLessonCard(lessonCard);
		clog.setTitle(title);
		clog.setCardId(cardId);
		clog.setContractId(contractId);

		Course course = new Course();
		course.setId(courseId);
		clog.setCourse(course);
		clog.setStudent(studentService.findOne(id));
		clog.setTerm(form.readString("term"));
		clog.setConsultant(userService.findUser(consultantId));
		User creator = (User) Profile.getInstance().getUser();
		clog.setNote(form.readString("note"));
		clog.setBook(form.readString("book"));

		clog.setCreator(creator);
		clog.setChecked(true);
		clog.setBook(book);
		clog.setChecker(creator);
		clog.setNote(note);

		clog.setSignCourse(null);

		clog = signCourseLogService.save(clog);
		signCourseService.checkSign(clog, true);

		setJson(JsonResultBuilder.success(true).msg("注册成功！").add("id", id).json());

		return JSON;
	}

	@Operation(result = FILL)
	public String checkSign() {
		String id = form.readString("id");

		SignCourseLog signCourseLog = signCourseLogService.findOne(id);

		put("signCourseLog", signCourseLog);

		Iterable<Course> courses = courseService.findAll();
		put("courses", courses);

		Iterable<User> consultants = consultantService.listConsultant();
		put("consultants", consultants);

		put("student", signCourseLog.getStudent());

		put("now", DateUtil.getAsString(DateUtil.now(), "yyyy-MM-dd"));

		return FILL;
	}

	@Autowired
	CourseService courseService;

	@Autowired
	ConsultantService consultantService;

	@Autowired
	FinancService financService;

	@Operation(result = JSON)
	public String doCheckSign() {
		// 学员编号
		String id = form.readString("id");

		SignCourseLog clog = signCourseLogService.findOne(form.readString("signCourseLogId"));

		String stuId = form.readString("stuId");

		if (StringUtils.isNotBlank(stuId)) {
			studentService.changeStuId(id, stuId);
		}

		String courseId = form.readString("courseId");
		String contractId = form.readString("contractId");
		String cardId = form.readString("cardId");
		String title = form.readString("title");
		String lessonCard = form.readString("lessonCard");
		int times = valueReader.name("times").defaultValue(0).intValue();

		String consultantId = form.readString("consultantId");

		Date beginTime = valueReader.name("beginTime").dateValue("yyyy-MM-dd");
		Date endTime = valueReader.name("endTime").dateValue("yyyy-MM-dd");

		Date signTime = valueReader.name("signTime").dateValue("yyyy-MM-dd");

		String book = form.readString("book");

		clog.setBook(book);

		clog.setEndTime(endTime);
		clog.setBeginTime(beginTime);
		clog.setTimes(times);
		clog.setLessonCard(lessonCard);
		clog.setTitle(title);
		clog.setCardId(cardId);
		clog.setContractId(contractId);

		Course course = new Course();
		course.setId(courseId);
		clog.setCourse(course);

		clog.getStudent().setId(id);
		clog.setTerm(form.readString("term"));
		clog.setConsultant(userService.findUser(consultantId));

		SignCourse signCourse = new SignCourse();
		clog.setSignCourse(signCourse);

		signCourseService.checkSign(clog, true);

		setJson(JsonResultBuilder.success(true).msg("合同确认成功！").add("id", id).json());
		return JSON;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public void setCheck(int check) {
		switch(check){
		case 0:
			checked=null;
			break;
		case 1:
			checked=true;
			break;
		case 2:
			checked=false;
		}
		this.check=check;
	}
	public void setConstansId(String[] constansId) {
		this.constansId = constansId;
	}
	
	public String getName() {
		return name;
	}
	public int getCheck() {
		return check;
	}
	public String[] getConstansId() {
		return constansId;
	}
}
